package com.itheima08;

import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author Xiao
 * @date 2021/7/26
 */
public class StudentTest {
    /**
     * 成绩排序：
     * 创建3个学生对象，属性为（姓名，语文成绩，数学成绩，英语成绩），
     * 按照总分从低到高输出到控制台。
     * 总分一样比语文、数学、英语，全都一样比姓名。
     */
    public static void main(String[] args) {
        Student stu1 = new Student("aLiang01", 90, 94, 98);
        Student stu2 = new Student("aLiang02", 94, 92, 94);
        Student stu3 = new Student("aLiang03", 90, 93, 99);
        Student stu4 = new Student("aLiang04", 93, 90, 95);
        Student stu5 = new Student("aLiang05", 97, 90, 91);
        Student stu6 = new Student("aLiang06", 94, 92, 94);
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
//                return compareIf(o1, o2);
                return compareResult(o1, o2);
            }
        });
        Collections.addAll(ts, stu1, stu2, stu3, stu4, stu5, stu6);
        for (Student stu : ts) {
            System.out.println(stu);
        }
    }

    /**用if条件判断重写Comparator中的compare方法*/
    public static int compareIf(Student o1, Student o2) {
        if (o1.getSumScore() != o2.getSumScore()) {
            return o1.getSumScore() - o2.getSumScore();
        }
        if (o1.getScoreChinese() != o2.getScoreChinese()) {
            return o1.getScoreChinese() - o2.getScoreChinese();
        }
        if (o1.getScoreMath() != o2.getScoreMath()) {
            return o1.getScoreMath() - o2.getScoreMath();
        }
        return o1.getName().compareTo(o2.getName());
    }

    /**用三元运算符重写Comparator中的compare方法*/
    public static int compareResult(Student o1, Student o2) {
        int result = o1.getSumScore() - o2.getSumScore();
        result = result == 0 ? o1.getScoreChinese() - o2.getScoreChinese() : result;
        result = result == 0 ? o1.getScoreMath() - o2.getScoreMath() : result;
        result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
        return result;
    }
}
